package com.limbo.showapp.pop;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.limbo.showapp.R;
import com.limbo.showapp.util.MyUtils;
import com.limbo.showapp.widget.MyPopupWindow;
import com.wang.avi.AVLoadingIndicatorView;


/**
 * Created by wangqi on 2018/8/30.
 */

public class LodingPopupWindow {

    private MyPopupWindow mPopupWindow;

    public LodingPopupWindow(final Activity activity) {
        if (mPopupWindow == null) {
            View contentView = LayoutInflater.from(activity).inflate(R.layout.pop_loding_view, null);
            AVLoadingIndicatorView avi = contentView.findViewById(R.id.avi);
            avi.show();

            mPopupWindow = new MyPopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, true);

            mPopupWindow.setBgAlpha(0.6f);

//        mPopupWindow.setAnimationStyle(animationStyle);
            // 设置PopupWindow的背景
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            // 设置PopupWindow是否能响应外部点击事件
            mPopupWindow.setOutsideTouchable(false);
            // 设置PopupWindow是否能响应点击事件
            mPopupWindow.setTouchable(true);
            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            MyUtils.setBackgroundAlpha(activity, 1f);
                        }
                    }, 200);
                }
            });
        }
    }


    public void show(View view) {
        if (mPopupWindow!=null){
            mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    public void dismiss(){
        if (mPopupWindow!=null){
            mPopupWindow.dismiss();
        }
    }

}
